#include "task.hpp"
#include "blockQueue.hpp"
#include <ctime>
#include <unistd.h>

void *consumer(void *args){
    blockQueue<task> *bq = static_cast<blockQueue<task> *> (args);
    while(1){
        task  t = bq->pop();
        t.run();
        printf("处理了一个任务\n\n");
    }
}

void *producer(void *args){
    blockQueue<task> *bq = static_cast<blockQueue<task> *> (args);
    while(1){
        int x = 10, a = rand() % x, b = rand() % x;
        char ope = s[rand() % 4];
        bq->push(task(a, b, ope));
        printf("生产了一个任务\n");
        sleep(1);
    }
}

int main(){
    srand(time(nullptr));
    pthread_t c[3],p[5];
    blockQueue<task> *bq = new blockQueue<task>();
    for (int i = 0; i < 3; i++)
    {
        pthread_create(c + i, nullptr, consumer, bq);
    }

    for (int i = 0; i < 5; i++)
    {
        pthread_create(p + i, nullptr, producer, bq);
    }

    for (int i = 0; i < 3; i++)
    {
        pthread_join(c[i], nullptr);
    }
    for (int i = 0; i < 5; i++)
    {
        pthread_join(p[i], nullptr);
    }
    delete bq;

    return 0;
}

